package vashion.azeroth.back.controller.item;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import vashion.azeroth.back.controller.base.BaseController;
import vashion.azeroth.core.result.ApiResult;
import vashion.azeroth.core.item.pojo.ItemBrandDO;
import vashion.azeroth.core.item.pojo.ItemForestBrandDO;
import vashion.azeroth.core.manager.forest.BaseForestManager;
import vashion.azeroth.core.manager.item.BaseItemBrandManager;
import vashion.azeroth.core.manager.item.BaseItemForestBrandManager;
import vashion.azeroth.core.item.pojo.BrandWithForestList;
import vashion.azeroth.core.item.pojo.ForestDO;

/**
 * Created by Loki on 16/1/17.
 *
 */

@Controller
@RequestMapping("/back/brand")
public class BackBrandController<T> extends BaseController {

	private final static Logger log = LoggerFactory.getLogger(BackBrandController.class);

	@Autowired
	BaseItemBrandManager baseItemBrandManager;
	
	@Autowired
	private BaseItemForestBrandManager baseItemForestBrandManager;
	
	@Autowired
	BaseForestManager baseForestManager;

	/**
	 * @param brandDO
	 * @param forestIds
	 * @param request
	 * @return 发布一个品牌，并且关联一些类目
	 */
	@RequestMapping("publishBrand.htm")
	@ResponseBody
	public ApiResult<ItemBrandDO> publishBrand(@ModelAttribute("brandDO") ItemBrandDO brandDO,
			@RequestParam(value = "forestIds", required = true) String forestIds, HttpServletRequest request) {
		ApiResult<Boolean> publishResult = baseItemBrandManager.publishBrand(brandDO, forestIds,
				getLoginUserId(request));
		if (publishResult.isSuccess() && publishResult.getData()) {
			return ApiResult.newSuccessResult();
		} else {
			return ApiResult.newErrorResult(publishResult.getErrorCode(), publishResult.getErrorMsg());
		}
	}
	
	/**
	 * @param brandDO
	 * @param forestIds
	 * @param request
	 * @return 更新一个品牌，并且关联一些类目
	 */
	@RequestMapping("updateBrand.htm")
	@ResponseBody
	public ApiResult<ItemBrandDO> updateBrand(@ModelAttribute("brandDO") ItemBrandDO brandDO,
			@RequestParam(value = "forestIds", required = true) String forestIds, HttpServletRequest request) {
		ApiResult<Boolean> publishResult = baseItemBrandManager.updateBrand(brandDO, forestIds,
				getLoginUserId(request));
		if (publishResult.isSuccess() && publishResult.getData()) {
			return ApiResult.newSuccessResult();
		} else {
			return ApiResult.newErrorResult(publishResult.getErrorCode(), publishResult.getErrorMsg());
		}
	}

	/**
	 * @param brandDO
	 * @param needForests
	 * @return 获取品牌列表，包含拥有的类目
	 */
	@RequestMapping("getBrandListWithForests.htm")
	@ResponseBody
	public ApiResult<List<BrandWithForestList>> getBrandListWithForests(@ModelAttribute("brandDO") ItemBrandDO brandDO,
			@RequestParam(value = "needForests", required = false) boolean needForests) {
		brandDO.setFirstRow((brandDO.getPageNum() - 1) * brandDO.getPageSize());
		ApiResult<List<BrandWithForestList>> getListResult = baseItemBrandManager.getBrandListWithForests(brandDO,
				needForests);
		return getListResult;
	}

	/**
	 * @param brandId
	 * @param needForests
	 * @return 获取品牌细节，包含拥有的类目
	 */
	@RequestMapping("getBrandDetailWithForests.htm")
	@ResponseBody
	public ApiResult<BrandWithForestList> getBrandDetailWithForests(
			@RequestParam(value = "brandId", required = true) long brandId,
			@RequestParam(value = "needForests", required = false) boolean needForests) {
		ApiResult<BrandWithForestList> getResult = baseItemBrandManager.getBrandDetailWithForests(brandId, needForests);
		return getResult;
	}

    /**
     * 冻结一个品牌
     * @param brandId
     * @param request
     * @return 冻结的结果
     */
    @RequestMapping("freezeBrand.htm")
    @ResponseBody
    public ApiResult<Boolean> freezeBrand(@RequestParam(value = "brandId", required = true) long brandId,
    		                              HttpServletRequest request) {
    	ApiResult<Boolean> freezeResult = baseItemBrandManager.freezeBrand(brandId, getLoginUserId(request));
    	
    	if (freezeResult.isSuccess() && freezeResult.getData()) {
            return ApiResult.newSuccessResult();
        } else {
            return ApiResult.newErrorResult(freezeResult.getErrorCode(), freezeResult.getErrorMsg());
        }
    }

    /**
     * 恢复一个品牌
     * @param brandId
     * @param request
     * @return 恢复的结果
     */
    @RequestMapping("resumeBrand.htm")
    @ResponseBody
    public ApiResult<Boolean> resumeBrand(@RequestParam(value = "brandId", required = true) long brandId,
    		                              HttpServletRequest request) {
    	ApiResult<Boolean> resumeResult = baseItemBrandManager.resumeBrand(brandId, getLoginUserId(request));
    	
    	if (resumeResult.isSuccess() && resumeResult.getData()) {
            return ApiResult.newSuccessResult();
        } else {
            return ApiResult.newErrorResult(resumeResult.getErrorCode(), resumeResult.getErrorMsg());
        }
    }
	
	/**
	 * 根据一个类目获取完整品牌列表
	 *
	 * @return
	 */
	@RequestMapping("getBrandListByForestId.htm")
	@ResponseBody
	public ApiResult<List<ItemForestBrandDO>> getBrandListByCategoryId(@ModelAttribute("itemForestBrandDO") ItemForestBrandDO itemForestBrandDO) {
		//如果是二级类目，获取父类目对于的品牌
		ForestDO forest = baseForestManager.get(itemForestBrandDO.getForestId());
		if (null != forest && null != forest.getParentId() && forest.getParentId() > 0) {
			itemForestBrandDO.setForestId(forest.getParentId());
		}
		return baseItemForestBrandManager.getBrandListByForestId(itemForestBrandDO);
	}
}
